<template>
  <div
    class="contract-right relative ease-out duration-150 border-l-1 border-slate-300"
    :class="[isShow ? 'translate-x-full' : '']"
  >
    <Menu v-model:selectedKeys="current" mode="horizontal" :items="items" />

    <KeepAlive>
      <template v-if="current.includes('seal')">
        <div v-loading="loading" class="p-4 flex flex-wrap gap-4">
          <div
            v-for="item in data"
            :key="item.id"
            class="w-[150px] min-h-[182px] border border-slate-300 rounded-md relative"
          >
            <div class="size-[150px] flex items-center justify-center cursor-pointer">
              <img class="size-[120px]" :src="'data:image/jpeg;base64,' + item.base" />
            </div>
            <p class="w-full text-center leading-[32px]">
              {{ item.fullName }}
            </p>
            <div class="absolute bottom-2 right-2 cursor-pointer" @click="handleDelStamp(item.id)">
              <Icon icon="ant-design:delete-twotone" />
            </div>
          </div>
        </div>
      </template>
    </KeepAlive>

    <div
      class="absolute -left-4 top-1/2 -translate-y-1/2 h-20 flex items-center bg-white rounded-full cursor-pointer shadow-sm hover:shadow-xl ease-out duration-300"
      @click="isShow = !isShow"
    >
      <Icon
        icon="eva:arrow-left-outline"
        :size="32"
        class="ease-in duration-200"
        :class="[isShow ? '' : 'rotate-180']"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { delContractStampApi, getContractStampList } from '@/api/contract/stamp';
  import Icon from '@/components/Icon/Icon.vue';
  import { Menu, message } from 'ant-design-vue';
  import { useRequest } from '@vben/hooks';
  import { h, ref } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { isNumber } from 'xe-utils';

  const emits = defineEmits(['change']);

  const items = [
    {
      key: 'seal',
      icon: () => h(Icon, { icon: 'mingcute:seal-line' }),
      label: '我的印章',
      title: '我的印章',
    },
  ];
  const current = ref(['seal']);
  const isShow = ref(false);

  const { data, loading, refresh } = useRequest(getContractStampList, { ready: true });
  const { createErrorModal } = useMessage();

  function handleDelStamp(id) {
    if (!isNumber(+id)) return;
    createErrorModal({
      title: '警告',
      content: '确定删除该印章？',
      onOk() {
        delContractStampApi(id).then((res) => {
          message.success('删除成功');
          refresh();
        });
      },
    });
  }
</script>

<style lang="less" scoped>
  .contract-right {
    position: fixed;
    top: @header-height;
    right: 0;
    width: 370px;
    height: 100%;
    background-color: #fff;
  }
</style>
